package test;

import domain.Student;

import java.util.ArrayList;
import java.util.Scanner;
public class StudentsManager {
    //实现思路,增删改查功能,对于删改等功能需要对应的访问索引功能来实现
    //首先实现界面
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    changeStudent(list);
                    break;
                case 4:
                    searchStuden(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用");//创建程序的出口
                    return;
                default:
                    System.out.println("您的输入有误,请检查重试");//使输入错误得到返回
                    break;
            }
        }
    }//创建添加方法!

    public static void addStudent(ArrayList<Student> list) {//明确对对象集合进行操作
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {//以死循环来检索重复学号
            System.out.println("请输入学生学号");
            id = sc.next();
            int index = matchStudent(list, id);
            if (index == -1) {
                break;
            } else {
                System.out.println("您输入的学号已存在!请重新输入!");
            }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生地址");
        String adress = sc.next();
        Student stu = new Student(id, name, age, adress);//进行添加
        list.add(stu);
        System.out.println("添加学生成功!");

    }

    public static void searchStuden(ArrayList<Student> list) {//创建搜寻学生方法
        if (list.size() == 0) {
            System.out.println("暂无学生信息,请录入后重试");
            return;
        } else {
            System.out.println("学号\t姓名\t年龄\t地址\t");
            for (int i = 0; i < list.size(); i++) {//遍历集合来获取
                Student stu = list.get(i);//创建一个学生对象来暂存信息
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAdress());//对信息进行输出
            }
        }
    }

    public static void deleteStudent(ArrayList<Student> list) {//
        System.out.println("请输入删除学生的学号");
        Scanner sc = new Scanner(System.in);
        String sid = sc.next();//创建删除学生方法
        int index = matchStudent(list, sid);
        {
            if (index == -1) {
                System.out.println("您输入的学号不存在,请检查后重试");
            } else {
                list.remove(index);
                System.out.println("删除成功!");
            }
        }
    }

    public static void changeStudent(ArrayList<Student> list) {//修改学生方法
        System.out.println("请输入修改学生的学号");
        Scanner sc = new Scanner(System.in);
        String sid = sc.next();
        int index = matchStudent(list, sid);
        if (index == -1) {
            System.out.println("您输入的学号不存在,请检查后重试");
        } else {
            Student stu = new Student();
            stu.setId(sid);
            System.out.println("请输入新的姓名");
            String name = sc.next();
            stu.setName(name);
            System.out.println("请输入新的年龄");
            int age = sc.nextInt();
            stu.setAge(age);
            System.out.println("请输入新的地址");
            String adress = sc.next();
            stu.setAdress(adress);
            list.set(index, stu);
            System.out.println("修改成功!");
        }
    }

    public static int matchStudent(ArrayList<Student> list, String sid) {//创建匹配学生方法
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);//找到元素
            String id = stu.getId();//获取id;
            int index = i;//匹配索引
            if (sid.equals(id)) {
                return index;//对比id.成功返回索引
            }
        }
        return -1;//对比失败,返回无元素
    }
}

